package com.example.whatrubbish.service.impl;

import com.example.whatrubbish.mapper.UserMapper;
import com.example.whatrubbish.model.Integration;
import com.example.whatrubbish.model.User;
import com.example.whatrubbish.service.IntegrationService;
import com.example.whatrubbish.service.UserService;
import com.example.whatrubbish.utils.DateFormatUtils;
import com.example.whatrubbish.vo.Criteria;
import com.example.whatrubbish.vo.UserWithIntegration;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    IntegrationService integrationService;

    @Override
    public Long selectSearchRecordCount(Criteria criteria) {
        return userMapper.selectSearchRecordCount(criteria);
    }

    @Override
    public List<UserWithIntegration> selectSearchRecord(Criteria criteria) {
        List<User> list = userMapper.selectSearchRecord(criteria);
        return list.stream()
                .map(user ->
                        new UserWithIntegration(user,
                                ObjectUtils.isNotEmpty(integrationService.selectByUserId(user.getUserId()))
                                        ? integrationService.selectByUserId(user.getUserId()).getScore() : 0))
                .collect(Collectors.toList());
    }

    @Override
    public int deleteByPrimaryKey(String userId) {
        return 0;
    }

    @Override
    public int insert(User record) {
        return 0;
    }

    @Transactional
    @Override
    public int insertSelective(User record) {
        record.setUserTime(new Date());
        integrationService.update(new Integration(null, 0, record.getUserId()));
        return userMapper.insertSelective(record);
    }

    @Override
    public UserWithIntegration findByPrimaryKey(String userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        if (!Objects.isNull(user)) {
            Integration integration = integrationService.selectByUserId(user.getUserId());
            Integer score = 0;
            if (Objects.isNull(integration)) {
                integration = new Integration(null, score, userId);
                integrationService.update(integration);
            } else {
                score = integration.getScore();
            }
            return new UserWithIntegration(user, score);
        }
        user = new User();
        return new UserWithIntegration(user, null);
    }

    @Override
    public User selectByPrimaryKey(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }
}
